﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Seagull.Generic;
using Seagull.Model.Stat;

namespace Seagull.DAL.Stat
{
    public class FrameStatFileReader
    {
        private readonly FileInfo _frameStatFile;

        public FrameStatFileReader(FileInfo frameStatFile)
        {
            _frameStatFile = frameStatFile;
        }

        public IEnumerable<Statistic> ReadAll()
        {
            if(!_frameStatFile.Exists)
            {
                yield break;
            }
            using(var fileStream = FileHelper.Open(() => _frameStatFile.OpenRead()))
            {
                int max;
                while((max = fileStream.ReadByte()) != -1)
                {
                    var avg = fileStream.ReadByte();
                    yield return new Statistic((byte)max, (byte)(avg != -1 ? avg : 0));
                }
            }
        }
    }
}
